package org.zfes.snowier.cloud.zbss.metting.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;

import com.google.common.collect.Lists;

import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.metting.biz.service.IMetCmsContentService;
import org.zfes.snowier.cloud.zbss.metting.biz.dao.MetCmsContentMapper;
import org.zfes.snowier.cloud.zbss.metting.biz.model.MetCmsContent;
import java.util.stream.Stream;
@Service
public class MetCmsContentServiceImpl implements IMetCmsContentService{
 	@Autowired
	private MetCmsContentMapper metCmsContentMapper;
 	//#1关于我们，2服务热线，3联系我们-公司邮箱，4联系我们-公司传真，5联系我们-公司地址，6联系我们-经纬度地址，7联系我们-咨询热线	8会议注册开通协议，
 	//9案例编辑，10问题指南--开通说明，11问题指南--常见问题，12资讯 #
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createMetCmsContent(MetCmsContent metCmsContent) {
		ZBeanUtil.setCreateInfo(metCmsContent);
		ZBeanUtil.validateBean(metCmsContent);
		if(metCmsContent.getId()!=null/*&&metCmsContent.getId() <= 8L*/) {
			updateMetCmsContent( metCmsContent);
		}else {
			metCmsContentMapper.insertSelective(metCmsContent);
		}
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateMetCmsContent(MetCmsContent metCmsContent) {
		ZBeanUtil.setUpdatedInfo(metCmsContent);
		ZBeanUtil.validateBean(metCmsContent);
		metCmsContentMapper.updateByPrimaryKeySelective(metCmsContent);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<MetCmsContent> loadMetCmsContentById(Long id) {
		if(id!=null){
			return Optional.ofNullable(metCmsContentMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<MetCmsContent> loadMetCmsContentByIds(java.util.List<Long> ids){
		if(ZObjectUtil.isEmpty(ids)) {
			return Lists.newArrayList();
		}
		return metCmsContentMapper.selectByIds(ids);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteMetCmsContent(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		if(id<=8) {
			return;
		}
		metCmsContentMapper.deleteByPrimaryKey(id);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMetCmsContentStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		metCmsContentMapper.updateStatus( id,  status) ;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadMetCmsContentDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(metCmsContentMapper.selectListCount(pm), metCmsContentMapper.selectMapListPage(pm));
		return ds;
	}
	
}